package com.jerry.shop.service.impl;

import com.jerry.shop.bean.Category;
import com.jerry.shop.mapper.CategoryMapper;
import com.jerry.shop.service.CategoryService;
import com.jerry.shop.utils.KeyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 类目
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public Category findOne(Integer id) {
        return categoryMapper.getCategoryById(id);
    }

    @Override
    public List<Category> findAll() {
        return categoryMapper.getCategoryList();
    }

    @Override
    public List<Category> findByCategoryTypeIn(List<Integer> categoryTypeList) {
        return categoryMapper.getCategoryListByTypeIn(categoryTypeList);
    }

    @Override
    public Category saveOrUpdate(Category category) {
        //如果categoryId为空, 说明是新增
        if (StringUtils.isEmpty(category.getId())) {
            if (categoryMapper.insertCategory(category) == 1) {
                return category;
            }
        } else {
            Category updateCategory = findOne(category.getId());
            BeanUtils.copyProperties(category, updateCategory);
            if (categoryMapper.updateCategory(updateCategory) == 1) {
                return updateCategory;
            }
        }
        return null;
    }

}
